from .basedata import *

class ModelListPanelGUI(wx.Panel, BaseData):

    def __init__(self, parent = None):
        BaseData.__init__(self)
        wx.Panel.__init__(self, parent, wx.ID_ANY)
        
        self._init_UI()  # 初始化界面布局
        self._init_Ref()

    def _init_Ref(self):
        '''初始化右击菜单项'''
        '''
            树控件右击选项
        '''
        self.popup_root_new_app = wx.NewIdRef() # 创建应用程序
        self.popup_root_export_database = wx.NewIdRef() # 导出数据库
        self.popup_root_migrate_database = wx.NewIdRef() # 数据源一键切换[结构+数据]
        self.popup_root_recycle_bin = wx.NewIdRef() # 回收站
        self.popup_root_new_model = wx.NewIdRef() # 创建模型
        self.popup_root_import_model = wx.NewIdRef() # 导入模型
        
        self.popup_node_delete_model = wx.NewIdRef() # 删除模型
        self.popup_node_export_model = wx.NewIdRef() # 导出模型
        self.popup_node_register_model = wx.NewIdRef() # 注册到后台
        self.popup_node_model_Meta = wx.NewIdRef() # Meta选项

        # self.popup_node_rest_framework = wx.NewIdRef() # Rest Framework
        self.popup_node_rf_Serializer = wx.NewIdRef() # serializers.Serializer
        self.popup_node_rf_ModelSerializer = wx.NewIdRef() # serializers.ModelSerializer

        self.popup_node_vscode_open_model = wx.NewIdRef() # VSCode 打开模型文件

        '''
            树型列表右击选项
        '''
        self.popup_treelist_add_field = wx.NewIdRef() # 新增字段
        self.popup_treelist_modify_field = wx.NewIdRef() # 修改字段
        self.popup_treelist_delete_field = wx.NewIdRef() # 删除字段
        self.popup_treelist_copy_field = wx.NewIdRef() # 拷贝字段
        
    def _init_UI(self):
        """初始化UI界面"""
        sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(sizer)
        self.SetAutoLayout(True)
        self.SetBackgroundColour(wx.WHITE) # CON_COLOR_MAIN

        self.panel = wx.Panel(self, wx.ID_ANY)
        self.panelSizer = wx.BoxSizer(wx.VERTICAL)
        self.panel.SetSizer(self.panelSizer)
        self.panelSizer.SetSizeHints(self.panel)
        sizer.Add(self.panel, 1, wx.EXPAND)

        self.splitViewsWindow = wx.SplitterWindow(self.panel, -1) # 定义分割容器
        self.panelSizer.Add(self.splitViewsWindow, 1, wx.EXPAND | wx.ALL, 0)

        self.leftPanel = wx.Panel(self.splitViewsWindow, style=wx.SUNKEN_BORDER)
        self.splitViewsWindow.Initialize(self.leftPanel)

        self.rightPanel = wx.Panel(self.splitViewsWindow, style=wx.SUNKEN_BORDER)
        self.splitViewsWindow.Initialize(self.rightPanel)
        self.rightPanel.SetBackgroundColour('gray')

        self.splitViewsWindow.SplitVertically(self.leftPanel, self.rightPanel, 220) # 垂直分割


        self._init_left_panel()
        self._init_right_panel()

    def _init_left_panel(self):
        '''初始化左面板'''
        leftPanelSizer = wx.BoxSizer(wx.VERTICAL)
        self.leftPanel.SetSizer(leftPanelSizer)

        '''
            树控件
        '''
        self.treeCtrl = ModelTreeCtrl(self.leftPanel, 
            style = wx.TR_HAS_BUTTONS
            # | wx.TR_EDIT_LABELS
            # | wx.TR_MULTIPLE
            # | wx.TR_HIDE_ROOT
        )
        leftPanelSizer.Add(self.treeCtrl, 1, wx.EXPAND | wx.ALL, 0)

        self.il = wx.ImageList(NODE_PIC_SIZE[0], NODE_PIC_SIZE[1])
        self.fldridx     = self.il.Add(wx.ArtProvider.GetBitmap(wx.ART_FOLDER,      wx.ART_OTHER, NODE_PIC_SIZE))
        self.fldropenidx = self.il.Add(wx.ArtProvider.GetBitmap(wx.ART_FOLDER_OPEN, wx.ART_OTHER, NODE_PIC_SIZE))
        self.fileidx     = self.il.Add(wx.ArtProvider.GetBitmap(wx.ART_NORMAL_FILE, wx.ART_OTHER, NODE_PIC_SIZE))
        self.infoidx     = self.il.Add(wx.ArtProvider.GetBitmap(wx.ART_INFORMATION, wx.ART_OTHER, NODE_PIC_SIZE))
        self.treeCtrl.SetImageList(self.il)

        '''
            树控件工具栏
        '''
        self.btnRefresh = buttons.GenButton(self.leftPanel, -1, '刷新')
        leftPanelSizer.Add(self.btnRefresh, 0, wx.EXPAND | wx.ALL, 0)
        self.btnRefresh.SetBackgroundColour(CON_COLOR_MAIN)
        self.btnRefresh.SetForegroundColour(CON_COLOR_PURE_WHITE)

    
    def _init_right_panel(self):
        """初始化右面板"""
        rightPanelSizer = wx.BoxSizer(wx.VERTICAL)
        self.rightPanel.SetSizer(rightPanelSizer)
        '''
            树形列表
        '''
        self.treeListData = wxdv.DataViewCtrl(self.rightPanel,
            style = wx.BORDER_THEME
            | wxdv.DV_ROW_LINES # 斑马纹
            | wxdv.DV_HORIZ_RULES
            | wxdv.DV_VERT_RULES
            | wxdv.DV_VARIABLE_LINE_HEIGHT
            # | wxdv.DV_MULTIPLE # 多选
            | wxdv.DV_SINGLE # 单选
        )
        rightPanelSizer.Add(self.treeListData, 1, wx.EXPAND | wx.ALL, 0)

        '''
            可控选项
        '''
        choicePanel = wx.Panel(self.rightPanel)
        choicePanelSizer = wx.BoxSizer(wx.HORIZONTAL)
        choicePanel.SetSizer(choicePanelSizer)
        rightPanelSizer.Add(choicePanel, 0, wx.EXPAND | wx.ALL, 0)
        choicePanel.SetBackgroundColour(CON_COLOR_MAIN)

        self.checkBoxHave = wx.CheckBox(choicePanel, -1, label = '隐藏未使用的字段类型')
        choicePanelSizer.Add(self.checkBoxHave, 0, wx.EXPAND | wx.ALL, 1)
        self.checkBoxHave.SetForegroundColour(wx.WHITE)
        if env.IsMac:
            self.checkBoxHave.Enable(False) # Mac上存在BUG，此功能被禁用

        choicePanelSizer.Add((5,5), 1, wx.EXPAND | wx.ALL, 1)

        self.btn_expand_all = wx.Button(choicePanel, -1, BUTTON_EXPAND_ALL)
        choicePanelSizer.Add(self.btn_expand_all, 0, wx.EXPAND | wx.ALL, 1)
